package com.docmanage.web.controller.am;

import com.docmanage.am.domain.AmAuthUser;
import com.docmanage.am.service.impl.AmAuthUserService;
import com.docmanage.common.annotation.Log;
import com.docmanage.common.core.controller.BaseController;
import com.docmanage.common.core.domain.AjaxResult;
import com.docmanage.common.core.page.TableDataInfo;
import com.docmanage.common.enums.BusinessType;
import com.docmanage.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 按用户授权Controller
 * 
 * @author pointLion
 * @date 2023-07-10
 */
@RestController
@RequestMapping("/am/authUser")
public class AmAuthUserController extends BaseController
{
    @Autowired
    private AmAuthUserService amAuthUserService;

    /**
     * 查询按用户授权列表
     */
    @PreAuthorize("@ss.hasPermi('am:authUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(AmAuthUser amAuthUser)
    {
        startPage();
        List<AmAuthUser> list = amAuthUserService.selectAmAuthUserList(amAuthUser);
        return getDataTable(list);
    }

    /**
     * 导出按用户授权列表
     */
    @PreAuthorize("@ss.hasPermi('am:authUser:export')")
    @Log(title = "按用户授权", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AmAuthUser amAuthUser)
    {
        List<AmAuthUser> list = amAuthUserService.selectAmAuthUserList(amAuthUser);
        ExcelUtil<AmAuthUser> util = new ExcelUtil<AmAuthUser>(AmAuthUser.class);
        util.exportExcel(response, list, "按用户授权数据");
    }

    /**
     * 获取按用户授权详细信息
     */
    @PreAuthorize("@ss.hasPermi('am:authUser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(amAuthUserService.selectAmAuthUserById(id));
    }

    /**
     * 新增按用户授权
     */
    @PreAuthorize("@ss.hasPermi('am:authUser:add')")
    @Log(title = "按用户授权", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AmAuthUser amAuthUser)
    {
        return toAjax(amAuthUserService.insertAmAuthUser(amAuthUser));
    }

    /**
     * 修改按用户授权
     */
    @PreAuthorize("@ss.hasPermi('am:authUser:edit')")
    @Log(title = "按用户授权", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AmAuthUser amAuthUser)
    {
        return toAjax(amAuthUserService.updateAmAuthUser(amAuthUser));
    }

    /**
     * 删除按用户授权
     */
    @PreAuthorize("@ss.hasPermi('am:authUser:remove')")
    @Log(title = "按用户授权", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(amAuthUserService.deleteAmAuthUserByIds(ids));
    }




    /****
     * 按照 用户ID查询，该角色下的所有文件夹权限
     * @param userId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('am:authRole:list')")
    @GetMapping("/listAuthFolder/{userId}")
    public AjaxResult listAuthFolder(@PathVariable("userId") Long userId)
    {
        List<AmAuthUser> list = amAuthUserService.listAuthFolder(userId);
        return AjaxResult.success(list);
    }


    /****
     * 给文件夹授权
     * @param auth
     * @return
     */
    @PostMapping("/allotFolder")
    public AjaxResult allotFolder(@RequestBody AmAuthUser auth){
        amAuthUserService.allotFolder(auth);
        return success();
    }



}
